from flask import jsonify,request,json


# 响应get请求
def response_get(code: int = 200, data: list | dict = {}, message="获取成功!"):
    res_dict = {
        "code": code,
        "data": data,
        "message": message
    }
    if not len(data):
        res_dict['message'] = '没有数据!'

    return jsonify(res_dict)


# 响应其他请求
def response_other(code: int = 200, message: str = '操作成功!'):
    return jsonify({
        "code": code,
        "message": message
    })


# 响应错误请求
def response_error(code: int = 400, err_msg: str = "参数错误!"):
    if code == 500 and not err_msg:
        err_msg = '服务器错误!'
    return jsonify({
        "code": code,
        "err_msg": err_msg
    })


# 获取body_data
def get_body_data():
    json_str = request.get_data()
    try:
        body_data = json.loads(json_str)
        return body_data
    except:
        return None
